// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ByteUtil.java

package com.solab.iso8583.common;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ByteUtil
{

	private final static String charset = "GBK";
	
	private static final Logger logger = LoggerFactory.getLogger(ByteUtil.class);
	private static String hexStr = "0123456789ABCDEF";
	public static final char BToA[] = "0123456789abcdef".toCharArray();
	public static final String nLine = "----------------------------------------------------------------------------";
	private static final int DECIMAL = 2;

	public ByteUtil()
	{
	}

	public static byte[] Int2ByteArray(int iSource, int iArrayLen)
	{
		byte bLocalArr[] = new byte[iArrayLen];
		for (int i = iArrayLen; i < 4 && i > 0; i--)
			bLocalArr[i - 1] = (byte)(iSource >> 8 * (iArrayLen - i) & 0xff);

		return bLocalArr;
	}

	public static String trace(byte inBytes[])
	{
		int j = 0;
		byte temp[] = new byte[76];
		bytesSet(temp, ' ');
		StringBuffer strc = new StringBuffer("");
		strc.append("----------------------------------------------------------------------------\n");
		for (int i = 0; i < inBytes.length; i++)
		{
			if (j == 0)
			{
				System.arraycopy(String.format("%03d: ", new Object[] {
					Integer.valueOf(i)
				}).getBytes(), 0, temp, 0, 5);
				System.arraycopy(String.format(":%03d", new Object[] {
					Integer.valueOf(i + 15)
				}).getBytes(), 0, temp, 72, 4);
			}
			System.arraycopy(String.format("%02X ", new Object[] {
				Byte.valueOf(inBytes[i])
			}).getBytes(), 0, temp, j * 3 + 5 + (j <= 7 ? 0 : 1), 3);
			if (inBytes[i] == 0)
				temp[j + 55 + (j <= 7 ? 0 : 1)] = 46;
			else
				temp[j + 55 + (j <= 7 ? 0 : 1)] = inBytes[i];
			if (++j == 16)
			{
				strc.append(new String(temp)).append("\n");
				bytesSet(temp, ' ');
				j = 0;
			}
		}

		if (j != 0)
		{
			strc.append(new String(temp)).append("\n");
			bytesSet(temp, ' ');
		}
		strc.append("----------------------------------------------------------------------------\n");
		System.out.println(strc.toString());
		logger.info((new StringBuilder(" trace = ")).append(strc.toString()).toString());
		return strc.toString();
	}

	private static void bytesSet(byte inBytes[], char fill)
	{
		if (inBytes.length == 0)
			return;
		for (int i = 0; i < inBytes.length; i++)
			inBytes[i] = (byte)fill;

	}

	public static byte[] byteAndByte(byte begin[], byte second[])
	{
		if (begin == null || begin.length == 0)
			if (second != null && second.length != 0)
				return second;
			else
				return null;
		if (second == null || second.length == 0)
			return begin;
		byte newTotal[] = new byte[begin.length + second.length];
		for (int i = 0; i < begin.length; i++)
			newTotal[i] = begin[i];

		for (int i = begin.length; i < second.length + begin.length; i++)
			newTotal[i] = second[i - begin.length];

		return newTotal;
	}

	public static byte[] getsubByte(byte total[], int begin, int length)
	{
		if (length > 0)
		{
			byte newTotal[] = new byte[length];
			for (int i = begin; i < length + begin; i++)
				newTotal[i - begin] = total[i];

			return newTotal;
		} else
		{
			return null;
		}
	}

	public static String fillString(String string, char filler, int totalLength, boolean atEnd) throws UnsupportedEncodingException
	{
		byte tempbyte[] = string.getBytes("GBK");
		int currentLength = tempbyte.length;
		int delta = totalLength - currentLength;
		for (int i = 0; i < delta; i++)
			if (atEnd)
				string = (new StringBuilder(String.valueOf(string))).append(filler).toString();
			else
				string = (new StringBuilder(String.valueOf(filler))).append(string).toString();

		return string;
	}

	public static byte[] hexStringToBytes(String hexString)
	{
		if (hexString == null || hexString.equals(""))
			return null;
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char hexChars[] = hexString.toCharArray();
		byte d[] = new byte[length];
		for (int i = 0; i < length; i++)
		{
			int pos = i * 2;
			d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}

		return d;
	}

	public static byte charToByte(char c)
	{
		return (byte)"0123456789ABCDEF".indexOf(c);
	}

	public static String bytesToHexString(byte src[])
	{
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0)
			return null;
		for (int i = 0; i < src.length; i++)
		{
			int v = src[i] & 0xff;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2)
				stringBuilder.append(0);
			stringBuilder.append(hv);
		}

		return stringBuilder.toString();
	}

	public static String bytesToString(byte src[], int begin, int length)
	{
		String str1 = null;
		StringBuilder sb = new StringBuilder("");
		if (begin == 0 && length == 0)
		{
			byte abyte0[] = src;
			int j = 0;
			for (int k = abyte0.length; j < k; j++)
			{
				byte element = abyte0[j];
				sb.append(String.valueOf(element));
			}

		} else
		{
			for (int i = begin; i < begin + length; i++)
			{
				byte element = src[i];
				sb.append(String.valueOf(element));
			}

		}
		str1 = sb.toString();
		System.out.println(str1);
		return str1;
	}

	public static String BinaryToHexString(byte bytes[])
	{
		String result = "";
		String hex = "";
		for (int i = 0; i < bytes.length; i++)
		{
			hex = String.valueOf(hexStr.charAt((bytes[i] & 0xf0) >> 4));
			hex = (new StringBuilder(String.valueOf(hex))).append(String.valueOf(hexStr.charAt(bytes[i] & 0xf))).toString();
			result = (new StringBuilder(String.valueOf(result))).append(hex).toString();
		}

		return result;
	}

	public static String binaryToDecimal(String binary)
	{
		StringBuffer buf = new StringBuffer();
		String strBinary[] = binary.split(" ");
		String as[] = strBinary;
		int i = 0;
		for (int j = as.length; i < j; i++)
		{
			String str = as[i];
			StringBuffer strBuf = new StringBuffer(str);
			char element[] = strBuf.reverse().toString().toCharArray();
			int digit = 0;
			int result = 0;
			char ac[] = element;
			int k = 0;
			for (int l = ac.length; k < l; k++)
			{
				char temp = ac[k];
				int intNumber = Integer.parseInt(Character.toString(temp));
				intNumber *= (int)Math.pow(2D, digit);
				result += intNumber;
				digit++;
			}

			buf.append(result);
			buf.append(" ");
		}

		return buf.toString();
	}

	public static String toStringHex(String s)
	{
		byte baKeyword[] = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++)
			try
			{
				baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

		try
		{
			s = new String(baKeyword, charset);
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
		}
		return s;
	}

	public static String cbcd2string(byte bytes[], boolean format, int length)
	{
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			temp.append((byte)((bytes[i] & 0xf0) >>> 4));
			temp.append((byte)(bytes[i] & 0xf));
		}

		String val = temp.toString();
		if (format)
			if (val.length() > length)
				return val.substring(0, 1).equalsIgnoreCase("0") ? val.substring(1) : val;
			else
				return val;
		if (val.length() > length)
			return val.substring(val.length() - 1, val.length()).equalsIgnoreCase("0") ? val.substring(0, val.length() - 1) : val;
		else
			return val;
	}

	public static byte[] str2cbcd(String asc, boolean format)
	{
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0)
			if (format)
			{
				asc = (new StringBuilder("0")).append(asc).toString();
				len = asc.length();
			} else
			{
				asc = (new StringBuilder(String.valueOf(asc))).append("0").toString();
				len = asc.length();
			}
		byte abt[] = new byte[len];
		if (len >= 2)
			len /= 2;
		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		for (int p = 0; p < asc.length() / 2; p++)
		{
			int j;
			if (abt[2 * p] >= 48 && abt[2 * p] <= 57)
				j = abt[2 * p] - 48;
			else
			if (abt[2 * p] >= 97 && abt[2 * p] <= 122)
				j = (abt[2 * p] - 97) + 10;
			else
				j = (abt[2 * p] - 65) + 10;
			int k;
			if (abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57)
				k = abt[2 * p + 1] - 48;
			else
			if (abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122)
				k = (abt[2 * p + 1] - 97) + 10;
			else
				k = (abt[2 * p + 1] - 65) + 10;
			int a = (j << 4) + k;
			byte b = (byte)a;
			bbt[p] = b;
		}

		return bbt;
	}

	public static byte[] str2Bcd(String s, boolean format)
	{
		if (s.length() % 2 != 0)
			if (format)
				s = (new StringBuilder("0")).append(s).toString();
			else
				s = (new StringBuilder(String.valueOf(s))).append("0").toString();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		char cs[] = s.toCharArray();
		for (int i = 0; i < cs.length; i += 2)
		{
			int high = cs[i] - 48;
			int low = cs[i + 1] - 48;
			baos.write(high << 4 | low);
		}

		return baos.toByteArray();
	}

	public static String bcd2Str(byte b[], boolean format, int length)
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
		{
			int h = ((b[i] & 0xff) >> 4) + 48;
			sb.append((char)h);
			int l = (b[i] & 0xf) + 48;
			sb.append((char)l);
		}

		String val = sb.toString();
		if (format)
			if (val.length() > length)
				return val.substring(0, 1).equalsIgnoreCase("0") ? val.substring(1) : val;
			else
				return val;
		if (val.length() > length)
			return val.substring(val.length() - 1, val.length()).equalsIgnoreCase("0") ? val.substring(0, val.length() - 1) : val;
		else
			return val;
	}

	public static String BCD2ASC(byte bytes[])
	{
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			int h = (bytes[i] & 0xf0) >>> 4;
			int l = bytes[i] & 0xf;
			temp.append(BToA[h]).append(BToA[l]);
		}

		return temp.toString();
	}

	public static void main(String args[])
		throws Exception
	{
		System.out.println(hexStringToBytes("DC4F8DA27B17CF17BC17306DF2FD41C7C5CA7C2F8325677280B03B0A656BABD6FF352A8A6064").length);
		System.out.println(bytesToHexString(hexStringToBytes("005B")));
		System.out.println(String.format("%04d", new Object[] {
			Integer.valueOf(256)
		}));
		System.out.println(fillString("256", '0', 4, false));
		System.out.println(Integer.toHexString(90));
		System.out.println(hexStringToBytes("D7C7CA5A868F1A5C71BF9D54DF28C5D191DFB7FB442EE6154CC00EC70262FCCEB63051BF2327"));
		System.out.println(hexStringToBytes("FF").length);
		System.out.println(bytesToHexString(hexStringToBytes("0058")));
		System.out.println((new StringBuilder("0058 = ")).append(Integer.parseInt(bytesToHexString(hexStringToBytes("0058")), 16)).toString());
		System.out.println(Integer.parseInt(toStringHex(BinaryToHexString("005a".getBytes())), 16));
		int len = 4;
		if (len % 2 != 0)
			System.out.println(len / 2 + 1);
		else
			System.out.println(len / 2);
		int fun[] = {
			0, 1, 2, 3, 4, 5, 6
		};
		System.arraycopy(fun, 0, fun, 3, 3);
		int ai[] = fun;
		int j = 0;
		for (int k = ai.length; j < k; j++)
		{
			int i = ai[j];
			System.out.print(i);
		}

		System.out.print("");
		System.out.println("088".getBytes().length);
		System.out.println((new StringBuilder("test = ")).append("00000000010000".length()).toString());
		System.out.println((new StringBuilder(" str2Bcd(011) length ")).append(str2Bcd("011", true).length).toString());
		System.out.println((new StringBuilder(" str2Bcd(011) ")).append(bcd2Str(str2Bcd("011", true), true, 3)).toString());
		System.out.println((new StringBuilder(" str2Bcd(00000001901) ")).append(bcd2Str(str2Bcd("00000001901", false), false, 11)).toString());
		System.out.println((new StringBuilder(" str2Bcd(09=0) ")).append(bcd2Str(str2Bcd("09=0", false), false, 4)).toString());
		System.out.println((new StringBuilder(" str2Bcd(09=0) ")).append(BinaryToHexString(str2Bcd("09=0", false))).toString());
		System.out.println((new StringBuilder("bytesToString = ")).append(toStringHex(BinaryToHexString("0003".getBytes()))).toString());
		String s = "0123";
		byte d[] = s.getBytes();
		String t = new String(d);
		System.out.println(t);
		String str = "88234234";
		System.out.println(toHexString(str));
	}

	public static String toHexString(String s)
	{
		String str = " ";
		for (int i = 0; i < s.length(); i++)
		{
			int ch = s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = (new StringBuilder(String.valueOf(str))).append(s4).toString();
		}

		return str;
	}

	public static void d()
	{
		System.out.println(Integer.toHexString(200));
		Integer.parseInt("8C", 16);
	}

}
